<?php include("../includes/header-admin.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
<script type="text/javascript">
<!--
function validateForm(){
    var elements = myForm.elements;
    for(var i = 0; i < elements.length; i++){
        if( elements[i].type == "text" && elements[i].value.length > 0 ) {
            var regex = /^[0-9]+$/;
            if( !regex.test(elements[i].value)){
              alert(elements[i].value + " is not a valid quantity.");
              return false;
            }
        }
    }
    return true;
}
//-->
</script>
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">

                <div id="sidebar">
                    <?php
					// include the corresponding navigation page
                    include("../../includes/navigation.php");
					?>     
                </div>

            <!-- main body -->
 			<div class="featured-post"> 
			
				<div class="post-title"><h2> <a href="index.php">Manage Store</a> - Process Orders</h2></div>
				<div class="post-entry">
				
				<form name="myForm" method="post" action="confirmOrders.php" onSubmit="return validateForm()"  >
				<?php
                // we only want the orders that are paid (ITEMS ORDERED means that they have already been ordered from equip store
                // and we do not want to process cancelled orders
                $sql = "SELECT * FROM orders WHERE status = 'PAID'";
                $orders = mysql_query($sql) or die(mysql_error()); 
                
                $products = array();
                
                while($order = mysql_fetch_assoc($orders)) {
                    $itemInfo = mysql_query("SELECT * FROM itemsOrdered WHERE orderID = '$order[orderID]'") or die(mysql_error()); 
                    while($item = mysql_fetch_assoc($itemInfo)) { 
                        $products[$item[productID]] += $item[quantity];
                    }
                }
                
                $i = 0;
                echo "<table width='65%'><tr><th>Product Name </th><th>Size</th><th>Total Quantity in Orders</th><th>Order this Item?</th>";
                foreach($products as $productID => $quantity) { 
                    // this is just to make the table pretty with alternating tr colours
                        if($i % 2) { 
                            $class = 'row1'; 
                        } 
                        else { 
                            $class = 'row2'; 
                        } 
                        $i++;
                     
                        $sql  = mysql_query("SELECT * FROM products WHERE productID = '$productID'");
                        $prod = mysql_fetch_assoc($sql);
                 ?>       
                     <tr class="<?php echo $class; ?>"> 
                            <td align="center"><?php echo $prod[name]; ?></td> 
                            <td align="center"><?php echo $prod[size]; ?></td> 
                            <td align="center"><input type="text" value="<?php echo $quantity; ?>" 
                            name="quantity <?php echo $productID; ?>" "onkeyup="checkField(this)"></td> 
                            <td align="center"><input type="checkbox" name="selected[]" value="<?php echo $productID; ?>" name="productID" checked></td>
                        </tr> 

                    <?php } ?>
                    <tr><td colspan=3 align="right"><input type="submit" value="Order Items"></td></tr>
                </table>
                </form>

				<!-- end of main -->
				</div>
			</div>

                <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>
